package com.fs.web;

import com.alibaba.fastjson.JSONObject;
import com.fs.pojo.Reply;
import com.fs.service.ReplyService;
import com.fs.utils.BeanFactories;
import com.fs.utils.JsonData;
import com.fs.vo.ReplyVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/reply/*")
public class ReplyServlet extends HttpServlet {

    private ReplyService replyService = BeanFactories.getBean(ReplyService.class);
    private HttpServletRequest req;
    private HttpServletResponse resp;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        this.req = request;
        this.resp = response;

        switch (req.getPathInfo()) {
            case "/getAllRepliesByDiscusstionId": getAllRepliesByDiscusstionId();break;
        }

    }

    private void getAllRepliesByDiscusstionId() throws IOException {

        String discusstionId = req.getParameter("discusstionId");

        List<ReplyVO> replyVOS = replyService.getAllRepliesByDiscusstionId(discusstionId);

        JsonData jsonData = JsonData.buildSuccess(replyVOS );

        resp.getWriter().print(JSONObject.toJSONString(jsonData));

    }
}
